//
//  MeViewController.m
//  waiMai
//
//  Created by LONG on 16/4/11.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MeViewController.h"
#import "MyTools.h"
#import "CollectionViewCell.h"
#import "MyInfoViewController.h"
@interface MeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong)UICollectionView *CollectionView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)UIImageView *headImageView;//头像

@end


@implementation MeViewController



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    
    
    MyInfoViewController *myInfoVC = [[MyInfoViewController alloc]init];
    [myInfoVC returnHeadImage:^(UIImage *headImage) {
        _headImageView.image = headImage;
        NSLog(@"头像传过来了");
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArray = [NSMutableArray array];
    
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:18],
       NSForegroundColorAttributeName:UIColorFromRGB(0x444444)}];
    
    //nav底部线消失
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBarTintColor:UIColorFromRGB(MYColor)];
    
    self.navigationItem.title = @"";
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate =(id)self;
    
    
    [self viewCreate];
}
- (void)viewCreate{
    self.view.backgroundColor = [UIColor whiteColor];
    
    //头视图
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0,0, WIDTH, 150 * MYHEIGHT)];
    headView.backgroundColor = UIColorFromRGB(0x00BFFF);
    [self.view addSubview:headView];
    headView.userInteractionEnabled = YES;
    
    //头像
    _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(305/2 *MYWIDTH, 0 *MYHEIGHT, 80 *MYHEIGHT, 80 *MYHEIGHT)];
    _headImageView.image = [UIImage imageNamed:@"头像"];
    [headView addSubview:_headImageView];
    _headImageView.layer.cornerRadius = 40 *MYHEIGHT;
    _headImageView.layer.masksToBounds = YES;
    
    _headImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *headImageViewClick = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headImageViewClick:)];
    [_headImageView addGestureRecognizer:headImageViewClick];
    
    //昵称
    UILabel *nickNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(280/2 *MYWIDTH, 80 *MYHEIGHT, 100 *MYWIDTH, 40)];
    nickNameLabel.text = @"圣体球霸亓大爷";
    nickNameLabel.font = [UIFont systemFontOfSize:13];
    nickNameLabel.textColor = [UIColor whiteColor];
    [headView addSubview:nickNameLabel];
    
    //昵称下面的三个按钮
    NSArray *buttonArray = @[@"代金券",@"收藏店铺",@"余额"];
    for (int i = 0; i <= 2; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(125 * i, 125,WIDTH / 3, 45)];
        button.backgroundColor = UIColorFromRGB(0x00BFFF);
        [headView addSubview:button];
        [button setTitle:buttonArray[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 100 + i;
    }
    
    
    
    //创建CollectionView
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    
    //设置显示形式 1、竖直滑动 2、水平滑动
    /*
     1、UICollectionViewScrollDirectionHorizontal 水平滑动
     2、UICollectionViewScrollDirectionVertical   竖直滑动
     */
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _CollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 180, WIDTH, HEIGHT - 180 - 64 - 49) collectionViewLayout:layout];
    
    _CollectionView.delegate = self;
    
    _CollectionView.dataSource = self;
    
    _CollectionView.showsVerticalScrollIndicator = NO;
    
    //默认为黑色
    _CollectionView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_CollectionView];
    
    //注册Collection
    [_CollectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"ID"];
}
#pragma mark 头像点击的手势
-(void)headImageViewClick:(UITapGestureRecognizer *)gtr{
    MyInfoViewController *myInfoVC = [[MyInfoViewController alloc]init];
    [self.navigationController pushViewController:myInfoVC animated:YES];
    NSLog(@"dianji");
}

#pragma mark 代金券 收藏店铺 余额 三个按钮的点击事件
-(void)buttonClick:(UIButton *)button{
    if (button.tag == 100) {
        NSLog(@"代金券");
    }if (button.tag == 101) {
        NSLog(@"收藏店铺");
    }if (button.tag == 102) {
        NSLog(@"余额");
    }
}
#pragma mark collectionView的方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 1) {
        return 13;
    }else{
        return 1;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ID" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        NSArray *buttonArray = @[@"累计返现",@"卡券包",@"银行卡"];
        for (int i = 0; i <= 2; i++) {
            UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(125 * i,15,WIDTH / 3, 45)];
            button.backgroundColor = UIColorFromRGB(MYGreen);
            [button setTitle:buttonArray[i] forState:UIControlStateNormal];
            [cell addSubview:button];
        }
    }
    if (indexPath.section == 1) {
        cell.TitleLabel.text = @"分享有礼";
        cell.backgroundColor = UIColorFromRGB(MYGray);
    }
    return cell;
}
//手动修改段数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
//调整Item的位置 使Item不紧挨着屏幕
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    //在原有基础上进行调整 上 左 下 右
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
//修改Item的大小 默认50 X 50
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(375/4, 375/4);
}
//设置水平间距与竖直间距 默认为10
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}


//选中方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"段:%ld 项:%ld",indexPath.section,indexPath.item);
}


#pragma mark 用于接收个人资料页传递过来的头像
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

}





@end


